# coding=utf-8
import maya.cmds as cmds
#import core as timelimit

def szfa_anim_curves_smooth():

	selected_anim_curves = cmds.keyframe(query=True, name=True)
	# print selected_anim_curves
	# current_selected = cmds.ls(sl=True)
	# for each_selected in current_selected:
	#	type = cmds.nodeType(each_selected)
	#	print each_selected +':'+ type
	#	node_type = 1 if type == "transform" or type == "joint" else 0
	#	print node_type
	# noinspection PyBroadException
	try:
		len(selected_anim_curves)
	except BaseException:
		cmds.error(u'请至少选择一个有动画的物体,或者动画曲线!')
 
	for each_curve in selected_anim_curves:
	
		keyframes_selected = cmds.keyframe(each_curve, query=True, time=(), selected=True)
		try:
			len(keyframes_selected)
		except BaseException:
			keyframes = cmds.keyframe(each_curve, query=True, time=())
		else:
			keyframes = cmds.keyframe(each_curve, query=True, time=(), selected=True)
			# print keyframes
		# else:
		# keyframes = cmds.keyframe(each_curve, query=True, time=(),selected=True)
		# print keyframes
	  
		if len(keyframes) < 3:
			keys_count = str(len(keyframes))
			# print each_curve+string_utf8+keys_count+string2_utf8
			print(each_curve + u' 只有或者只选择了' + keys_count + u'帧动画,跳过此曲线(至少需要三帧动画).')
			continue
	  
		else:
			# print each_curve+': '+str(keyframes)
			
			duplicated_anim_curve = cmds.duplicate(each_curve, name=each_curve + '_temp')
		   
			index = 1
			while index < len(keyframes) - 1:
				# print keyframes[index]
			  
				last_frame_value = cmds.keyframe(each_curve, query=True,
												 time=(keyframes[index - 1], keyframes[index - 1]), valueChange=True)
				# print last_frame_value
			  
				current_frame_value = cmds.keyframe(each_curve, query=True, time=(keyframes[index], keyframes[index]),
													valueChange=True)
				# print current_frame_value
			   
				next_frame_value = cmds.keyframe(each_curve, query=True,
												 time=(keyframes[index + 1], keyframes[index + 1]), valueChange=True)
				# print next_frame_value
			   
				average_value = (last_frame_value[0] + current_frame_value[0] + next_frame_value[0]) / 3
				# duplicated_anim_curve = cmds.duplicate[each_curve,name=each_curve+'_temp']
			   
				cmds.keyframe(duplicated_anim_curve, absolute=True, time=(keyframes[index], keyframes[index]),
							  valueChange=average_value)
				index += 1
		   
			index = 1
			while index < len(keyframes) - 1:
			 
				current_frame_value_duplicate_curve = cmds.keyframe(duplicated_anim_curve, query=True,
																	time=(keyframes[index], keyframes[index]),
																	valueChange=True)
				current_frame_value_orig_curve = cmds.keyframe(each_curve, query=True,
															   time=(keyframes[index], keyframes[index]),
															   valueChange=True)
				deviation_value = current_frame_value_orig_curve[0] - current_frame_value_duplicate_curve[0]
				# print D-value
				smooth_strength = cmds.floatSliderGrp('floatSlider_MDF', query=True, v=True)
				final_value = current_frame_value_orig_curve[0] - deviation_value * smooth_strength
			 

				cmds.keyframe(each_curve, absolute=True, time=(keyframes[index], keyframes[index]),
							  valueChange=final_value)
				index += 1
		   
			cmds.delete(duplicated_anim_curve)
		   
#if timelimit.ProductActivation().Win() == 0:
#	pass
if cmds.window('window_main_MDF', q=1, ex=1):
	cmds.deleteUI('window_main_MDF')
cmds.window('window_main_MDF', ret=1, mnb=0, mxb=0, t=u'动补数据抖动修复工具窗口', tb=1, vis=1, s=0, mb=1)

cmds.formLayout('formLayout_main_MDF', en=1, w=280, h=53, vis=1)

cmds.separator('separator01_MDF', st='single', en=1, vis=1, h=2, w=280, bgc=[0.0, 0.0, 0.0], hr=1)
cmds.floatSliderGrp('floatSlider_MDF', v=1.0, en=1, w=190, cw=[[2, 40], [1, 55]], pre=2, max=1.0, f=1, s=0.1, vis=1,
					l=u'圆滑强度', h=40, min=0.0, ann=u'拖动滑杆或者直接填入数值(0-1)来设置圆滑强度')
cmds.button('button_MDF', w=66, h=40, c='szfa_anim_curves_smooth()',
			bgc=[0.02439917601281758, 0.2574044403753719, 0.2684977492942702], l=u'修复抖动', ann=u'开始圆滑动画曲线')
cmds.separator('separator02_MDF', st='single', en=1, w=280, h=2, vis=1, bgc=[0.0, 0.0, 0.0], hr=1)

cmds.formLayout('formLayout_main_MDF', e=1, af=[[u'separator01_MDF', 'left', 0], ['separator01_MDF', 'top', 1]],
				ap=[['separator02_MDF', 'top', 42, 10], ['button_MDF', 'left', 185, 5], ['button_MDF', 'top', 3, 5],
					['floatSlider_MDF', 'top', 2, 5]])

cmds.showWindow('window_main_MDF')
